package com.woniu.lifeservice.controller;

import com.woniu.common.annotation.LoginUser;
import com.woniu.common.entity.Result;
import com.woniu.lifeservice.mapper.ServiceDetailsMapper;
import com.woniu.lifeservice.pojo.ServiceDetails;
import com.woniu.lifeservice.service.IServiceDetailsService;
import com.woniu.lifeservice.vo.ServiceDetailsVO;
import com.woniu.user.pojo.User;
import com.woniu.user.vo.UserLoginVO;
import com.woniu.user.vo.UserMessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 服务详情表 前端控制器
 * </p>
 *
 * @author hushunlai
 * @since 2023-05-22
 */
@RestController
@RequestMapping("/serviceDetails")
@Api(tags = "服务管理")
@Slf4j
public class ServiceDetailsController {

    @Autowired
    private IServiceDetailsService serviceDetailsService;

    @ApiOperation("服务名称查询")
    @GetMapping("/listSearch/{serviceTitle}")
    public Result listSearch(@PathVariable String serviceTitle){
        List<ServiceDetailsVO> serviceDetailsVOList = serviceDetailsService.listSearch(serviceTitle);
        return Result.ok(serviceDetailsVOList);
    }

    @ApiOperation("用户名称查询")
    @GetMapping("/nameSearch/{userName}")
    public Result nameSearch(@PathVariable String userName){
        List<ServiceDetailsVO> serviceDetailsVOList = serviceDetailsService.nameSearch(userName);
        return Result.ok(serviceDetailsVOList);
    }

    @ApiOperation("按浏览人数查询")
    @GetMapping("/visitorsRank")
    public Result visitorsRank(){
        List<ServiceDetailsVO> serviceDetailsVOList = serviceDetailsService.visitorRank();
        return Result.ok(serviceDetailsVOList);
    }

    @ApiOperation("按最新发帖查询")
    @GetMapping("/latestPosting")
    public Result latestPosting(){
        List<ServiceDetailsVO> serviceDetailsVOList = serviceDetailsService.latestPosting();
        return Result.ok(serviceDetailsVOList);
    }

    @ApiOperation("热门推荐")
    @GetMapping("/recommend")
    public Result recommend(){
        List<ServiceDetailsVO> serviceDetailsVOList = serviceDetailsService.recommend();
        return Result.ok(serviceDetailsVOList);
    }

    @ApiOperation("用户详细信息")
    @GetMapping("/userMessage")
    public Result userMessage(@PathVariable Integer userId){
        List<UserMessageVO> userMessageVOList = serviceDetailsService.userMessage(userId);
        return Result.ok(userMessageVOList);
    }

    @ApiOperation("查询详情页")
    @GetMapping("/detailsPage/{serviceId}")
    public Result detailsPage(@PathVariable Integer serviceId){
        List<ServiceDetails> serviceDetailsList = serviceDetailsService.detailsPage(serviceId);
        return Result.ok(serviceDetailsList);
    }

    @ApiOperation("按分类查询")
    @GetMapping("/categorySearch/{userName}")
    public Result categorySearch(@PathVariable String service_category_second){
        List<ServiceDetailsVO> serviceDetailsVOList = serviceDetailsService.categorySearch(service_category_second);
        return Result.ok(serviceDetailsVOList);
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save(@RequestBody ServiceDetails serviceDetails,@LoginUser @ApiIgnore UserLoginVO userLoginVO){
        ServiceDetails service = serviceDetailsService.add(serviceDetails, userLoginVO);
        System.out.println(userLoginVO.getId());
        return new Result(200,"发布成功",service);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/delete")
    public Result delete(@PathVariable List<Integer> ids){
        Boolean flag = serviceDetailsService.removeBatchByIds(ids);
        return Result.commonByFlag(flag);
    }
}
